%!TEX program = xelatex
%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

\documentclass[12pt,t,aspectratio=169,mathserif]{beamer}
%Other possible values are: 1610, 149, 54, 43 and 32. By default, it is to 128mm by 96mm(4:3).
%run XeLaTeX to compile.

\input{wang-slides-preamble.tex}

\begin{document}

%\title{应用数学入门系列讲座\\ \vspace{0.3cm} 第1讲：}
\title{傅立叶变换与图像压缩}
%(1.1-1.2) 
%\institute{上海立信会计金融学院}
\author{LQW}
%\date{}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{6.1.1. }
\begin{frame}{内容提要}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\vspace{1cm}

{\color{red}
用傅立叶变换方法，减小图像的存储空间，编程实现一个例子。
%课堂练习：把问题1-10做在作业本上，编程部分除外。

}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{6.1.1. }
\begin{frame}{目录}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

%\begin{multicols}{2}
\begin{enumerate}
%\item[1.1.] 傅立叶变换的定义
%\item[1.2.] 傅立叶逆变换的定义与存在性
%\item[1.3.] 一维的离散余弦变换DCT
%\item[1.4.] 一维DCT的矩阵形式
%\item[1.5.] 一维DCT的基函数
\item[1.] 傅立叶级数展开
\item[2.] 一维傅立叶变换
\item[3.] 一维离散余弦变换DCT
\item[4.] 根据二维DCT来存储图像的例子
\item[5.] 另一个例子：两张 JPG 图片
%\item[4.] 根据二维DCT来存储图像的理论
%\item[1.6.] Processing Steps for DCT-Based Coding
%\item[1.7.] 8x8 FDCT and IDCT
%\item[1.8.] 8x8 FDCT and IDCT
%\item[1.9.] Discrete Fourier Transform
%\item[1.10.] DC coefficient \& AC coefficients
%\item[1.11.] Lossless Compression
\end{enumerate}
%\end{multicols}


\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{6.1.1. }
%\begin{frame}{目录 2}
%
%\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%%每页详细内容
%
%%\begin{multicols}{2}
%\begin{enumerate}
%\item[1.12.] Research in fast DCT algorithms is ongoing
%\item[1.13.] Independently designed implementations
%\item[1.14.]  8-bit and 12-bit per component
%\item[1.15.] Quantization is fundamentally lossy
%\item[1.16.] Quantization algorithm
% \item[1.17.] Choose a quantization table
%\item[1.18.] The DC coefficient is treated separately
%\item[1.19.] zig-zag sequence
%\item[1.20.] Entropy Coding
%\item[1.21.] JPEG does not specify Huffman table
%\item[1.22.] Arithmetic coding
%\end{enumerate}
%%\end{multicols}
%
%\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{6.1.1. }
%\begin{frame}{目录 3}
%
%\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%%每页详细内容
%
%%\begin{multicols}{2}
%\begin{enumerate}
%\item[1.23.] Compression and Picture Quality
%\item[1.24.] Baseline Encoding Example
%\item[1.25.] DCT and Quantization Examples
%\item[1.26.] Example: DCT and Quantization (c) - (f)
%\item[1.27.] Example: the DC term
%\item[1.28.] Example: the AC coefficients
%\item[1.29.] Example: the VLCs from the informational annex
%\item[1.30.] Example: the final bit-stream
%\item[1.31.] JPEG: average quality \& lowest quality 
%\end{enumerate}
%%\end{multicols}
%
%\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.1. }
\begin{frame}{1.1. 傅立叶级数的定义}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
%\item 定义：
\item {\color{red}问题1：写出周期为 $2\pi$ 的函数的傅立叶级数的展开公式。}

\item 解答：设 $f(x)$ 是一个周期为 $2\pi$ 的周期函数，则其傅立叶级数展开为
\begin{eqnarray*}
f(x)  \,\,\sim\,\,  \frac{a_0}{2} + \sum\limits_{n=1}^{\infty} (a_n\cos nx + b_n\sin nx), 
\end{eqnarray*}
其中的系数由下述公式给出，
\begin{eqnarray*}
a_n &=& \frac{2}{2\pi} \int_{-\pi}^{\pi} f(x)\cos nx dx, (n=0,1,2,\cdots), \\
b_n &=& \frac{2}{2\pi} \int_{-\pi}^{\pi} f(x)\sin nx dx, (n=1,2,\cdots).
\end{eqnarray*}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.1. }
\begin{frame}{1.2. 傅立叶级数的一个例子}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
%\item 定义：
\item {\color{red}问题2：求下述函数的傅立叶展开，前四项记为 $f_4(x)$, 
\begin{eqnarray*}
f(x)=\left\{ 
\begin{array}{ll} 
-1,& -\pi<x<0, \\ 
0, &x=0, \\ 
1,& 0<x<\pi. 
\end{array}\right.
\end{eqnarray*}
}

\item 解答：按照傅立叶级数展开的定义，计算可得
\begin{eqnarray*}
f_4(x)  =  \frac{4}{\pi} \left( \sin x + \frac{1}{3}\sin 3x + \frac{1}{5}\sin 5x + \frac{1}{7}\sin 7x \right).
\end{eqnarray*}

\item 注：从下一页的图像，我们发现这个函数的傅立叶级数展开将收敛到原来的周期函数。即 $\lim\limits_{n\to\infty} f_n(x) = f(x)$. 



\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.1. }
\begin{frame}{1.3. 傅立叶系数的计算过程}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
\item 正弦函数 $\sin(3x)$ 的系数：

\begin{eqnarray*}
b_3 &=& \frac{\langle f(x),\sin(3x) \rangle}{\langle \sin(3x),\sin(3x) \rangle} 
= \frac{\int_{-\pi}^{\pi} f(x)\sin(3x)dx }{ \int_{-\pi}^{\pi} \sin(3x)\sin(3x)dx } 
= \frac{2\int_{0}^{\pi} \sin(3x)dx }{ 2\int_{0}^{\pi} \frac{1-\cos(6x)}{2}dx }  \\ 
&=& \frac{ - \frac{2}{3} \cos(3x)\mid_{0}^{\pi}}{x- \frac{1}{6}\sin(6x) \mid_{0}^{\pi}} 
= \frac{4/3}{\pi} = \frac{4}{3\pi}. 
\end{eqnarray*}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.1. }
\begin{frame}{1.4. 傅立叶展开的图像}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
%\item 定义：

\item {\color{red}问题3：接问题2，编写程序画出函数 $f(x)$ 与 $f_4(x)$ 的图像。}

 \begin{figure}
 \centering
 \includegraphics[height=0.6\textheight, width=0.9\textwidth]{square-wave-and-four-sines.png}
% \caption{ }
 \end{figure}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.1. }
\begin{frame}{1.5. 傅立叶级数的收敛性}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item {\color{red}问题4：周期函数 $f(x)$ 符合什么条件的时候，其傅立叶级数展开 $f_n(x)$ 将收敛到原函数，即有
\[
\lim\limits_{n\to\infty} f_n(x) = f(x) ?
\]
}

\item 解答：

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{1.6. 问题3的程序代码（R语言） }
%\begin{frame}{1.4. 问题4的程序代码 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{lstlisting}[language=R]
x<-seq(0,pi,by=pi/10)   #将区间分成10等分
y<-rep(1,length(x))   #重复11个1
xx<-c(x-2*pi,x-pi,x,x+pi,x+2*pi)   #规定横坐标取值范围
yy1<-c(y,-y,y,-y,y)   #规定纵坐标取值范围，左边的函数
yy2<-(sin(xx)+sin(3*xx)/3+sin(5*xx)/5+sin(7*xx)/7)*4/pi  
#右边的函数

par(mfrow=c(1,2))   #接下来画左右两个子图
plot(xx,yy1,type='l',xlim=c(-2*pi,3*pi),ylim=c(-1.2,1.2),
    col='red')
abline(h=0,v=0)   #画两个坐标轴
plot(xx,yy2,type='l',xlim=c(-2*pi,3*pi),ylim=c(-1.2,1.2),
    col='blue')
abline(h=0,v=0)   #画两个坐标轴
\end{lstlisting}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{1.7. 问题3的程序代码（Python） }
%\begin{frame}{1.4. 问题4的程序代码 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
import numpy as np   #载入数值计算程序包
import matplotlib.pyplot as plt   #载入画图程序包

x=np.linspace(-np.pi, np.pi, 101)   #将区间100等分
y=np.ones_like(x)   #定义全1向量
y[x<0]=-1   #修改向量 y 中的部分取值
y1=(np.sin(x)+np.sin(3*x)/3+np.sin(5*x)/5+np.sin(7*x)/7)*4/np.pi

plt.subplot(121)   #接下来画左右两个子图的第一个图
plt.plot(x,y,'b-',lw=2)
plt.subplot(122)   #接下来画左右两个子图的第二个图
plt.plot(x,y1,'b-',lw=2)
\end{python}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.1. }
\begin{frame}{2.1. 傅立叶变换的定义}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
%\item 定义：
\item {\color{red}问题5：什么是傅立叶变换？}

\item 解答：设 $f:\mathbb{R}\to\mathbb{R}$ 是一个定义在实数轴上的实值函数，设 $f$ 是绝对可积的。则 $f$ 的傅立叶变换是指函数 $g: \mathbb{R}\to\mathbb{C}$, 由下式定义
\begin{eqnarray*}
g(t) = \frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty} f(x)e^{-itx}dx.
\end{eqnarray*}

\item 记号：函数 $f(x)$ 的傅立叶变换 $g(t)$ 经常记为 $\mathcal{F}[f](t)$ 或者 $\hat{f}(t)$. 

\begin{eqnarray*}
\begin{bmatrix}  \\ g(t) \\  \\  \\  \end{bmatrix}
=
%\frac{1}{\sqrt{2\pi}}
\begin{bmatrix}
  - & - & - & - \\  
  - & e^{-itx} & - & - \\  
  - & - & - & - \\  
  - & - & - & - \\  
\end{bmatrix}
\begin{bmatrix}  \\ f(x) \\  \\  \\  \end{bmatrix}. 
\end{eqnarray*}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.2. }
\begin{frame}{2.2. 傅立叶逆变换的定义与存在性}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
\item 问题：什么是傅立叶逆变换？它什么时候存在？

\item 定理：设 $f:\mathbb{R}\to\mathbb{R}$ 是一个函数，设 $g(t)$ 是它的傅立叶变换。
\begin{enumerate}
\item 设 $f$ 是绝对可积的连续函数，则 
\begin{eqnarray*}
f(x) = \frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty} g(t)e^{itx}dt.
\end{eqnarray*}

\item 设 $f$ 绝对可积，而且在任意的有限区间内， $f(x)$ 和 $f'(x)$ 都是分段连续的，最多只有有限个极值点和不连续点，则
\begin{eqnarray*}
\frac{f(x+0)+f(x-0)}{2} = \frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty} g(t)e^{itx}dt.
\end{eqnarray*}

\end{enumerate}

%\item 证明或参考文献：Dirichlet 的论文。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.2. }
\begin{frame}{2.3. 傅立叶级数与傅立叶变换的联系}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  从一个函数 $f(x)$ 出发：
\begin{itemize}
\item  傅立叶级数：得到它在 $\{\cos(nx), \sin(nx)\}$ 上的系数 $\{a_n,b_n\}$. 
\item  傅立叶变换：得到它在 $\cos(tx)+i\sin(tx)$ 上的系数 $g(t)$. 
\end{itemize}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{2.4. 一个例子}
%\begin{frame}{2.4. 一个例子}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  画出 $f(x)=2\sin(x)+3\sin(2x), 0\le x\le 6\pi$ 的图像。

\item  程序：

\begin{python}
import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,6*np.pi,121)
y1=np.sin(x)*2; y2=np.sin(2*x)*3; y3=y1+y2

fig=plt.figure()
ax=fig.add_subplot(311); ax.plot(x,y1,'--')
bx=fig.add_subplot(312); bx.plot(x,y2,'--')
cx=fig.add_subplot(313); cx.plot(x,y3,'-')
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.2. }
\begin{frame}{2.5. 一个例子：两个正弦函数的和}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

 \begin{figure}
 \centering
 \includegraphics[height=0.75\textheight, width=0.9\textwidth]{fourier-series-example-2-4.png}
% \caption{ }
 \end{figure}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.3. }
\begin{frame}{3.1. 一维的离散余弦变换DCT}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
\item 定义：长度为 $N$ 的一维数组 $(f(0),f(1),\cdots,f(N-1))$ 的余弦变换是指另一个长度为 $N$ 的一维数组
 $(g(0),g(1),\cdots,g(N-1))$, 其中每一个分量为
\begin{eqnarray*}
g(t) = a(t) \sum\limits_{x=0}^{N-1}f(x)\cos \left[ \frac{\pi(2x+1)t}{2N} \right].
\end{eqnarray*}
其中 $a(0)=\sqrt{1/N}$, $a(1)=a(2)=\cdots=a(N-1)=\sqrt{2/N}$.

\vspace{0.5cm}

%\item {\color{red}问题1：如何看出傅立叶变换与离散余弦变换的共同点？}

%\item 解答：

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.4. }
\begin{frame}{3.2. 一维DCT的矩阵形式}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
\item 问题：用矩阵形式写出 $N=3$ 时的变换公式。

\item 解答：从 $f$ 到 $g$ 的变换系数组成一个正交矩阵，

\begin{eqnarray*}
\begin{bmatrix} g(0) \\ g(1) \\ g(2) \end{bmatrix}
=
\begin{bmatrix}  \sqrt{1/3}& \sqrt{1/3} & \sqrt{1/3}  \\  
\sqrt{2/3}\cos(\pi/6) & \sqrt{2/3}\cos(3\pi/6) & \sqrt{2/3}\cos(5\pi/6)  \\  
\sqrt{2/3}\cos(2\pi/6) & \sqrt{2/3}\cos(6\pi/6) & \sqrt{2/3}\cos(10\pi/6)  \\  
\end{bmatrix}
\begin{bmatrix} f(0) \\ f(1) \\ f(2) \end{bmatrix}.
\end{eqnarray*}

\vspace{0.5cm}

\item {\color{red}问题6：用矩阵形式写出 $N=4$ 时的变换公式。}
%\item {\color{red}问题7：写出一维离散余弦变换的逆变换的公式。}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.2. }
\begin{frame}{3.3. 变换矩阵（$N=8$） }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

 \begin{figure}
 \centering
 \includegraphics[height=0.7\textheight, width=0.45\textwidth]{dct-orth-matrix-3-3.png}
% \caption{ }
 \end{figure}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.5. }
\begin{frame}{3.4. 一维DCT的基函数（变换矩阵的每一行）}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
\item {\color{red}问题7：画出 $N=8$ 时的离散余弦变换的基函数。\\ 
即对每个 $t=0,1,\cdots,N-1$, 画出变换矩阵的每一行向量。}

\end{itemize}

 \begin{figure}
 \centering
 \includegraphics[height=0.6\textheight, width=0.4\textwidth]{dct-base-eight-vectors.png}
 \hspace{0.3cm}
 \includegraphics[height=0.6\textheight, width=0.4\textwidth]{dct-base-eight-vectors-hist.png}
% \caption{ }
 \end{figure}
 
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.5. 问题7的程序代码（R语言） }
%\begin{frame}{2.4. 问题7的程序代码}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{lstlisting}[language={R}]
a<-c(sqrt(1/8),rep(sqrt(2/8),7))   #定义系数向量
for (t in 1:8)   #按行循环，共8行
for (x in 1:8)   #对每行，计算DCT矩阵的分量的值
A[t,x]<-a[t]*cos(pi*(2*x-1)*(t-1)/16)

par(mfrow=c(4,2),mex=0.8,mar=c(3,3,2,1)+0.1)   
#接下来画出4x2共8个子图

for (t in 1:8)
plot(A[t,],type='b',lwd=2)   #画出DCT矩阵的每个行向量的点线图
\end{lstlisting}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.6. 问题7的程序代码（Python） }
%\begin{frame}{2.4. 问题7的程序代码}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
a=[np.sqrt(1/8)]   #定义一个列表，只有一个元素
a.extend(np.sqrt(2/8)*np.ones(7))   #附加其余7个元素

A=np.zeros((8,8))   #定义全零矩阵
for t in range(8):   #将变量t从0到7进行迭代
    for x in range(8):   #将变量x从0到7进行迭代
        A[t,x]=a[t]*np.cos(np.pi*(2*x+1)*t/16)  #给矩阵赋值

plt.subplot(421)
plt.plot(A[0,:],'b--.',lw=2)   #画出DCT矩阵的第一行
plt.subplot(422)
plt.plot(A[1,:],'b--.',lw=2)   #画出DCT矩阵的第二行
...
\end{python}



\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{3.6. 二维的离散余弦变换}
\begin{frame}{4.1. 二维的离散余弦变换DCT}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
\item 定义：阶数为 $N\times N$ 的二维数组 $\{f(x,y), x,y=0,1,\cdots,N-1\}$ 的离散余弦变换是指另一个 $N\times N$ 的二维数组 $\{F(u,v), u,v=0,1,\cdots,N-1\}$, 其中
{\color{red}
\begin{eqnarray*}
F(u,v) = a(u)a(v) \sum\limits_{x=0}^{N-1} \sum\limits_{y=0}^{N-1} f(x,y)
\cos \left[ \frac{\pi(2x+1)u}{2N} \right] 
\cos \left[ \frac{\pi(2y+1)v}{2N} \right].
\end{eqnarray*}
}
其中 $a(0)=\sqrt{1/N}$, $a(2)=\cdots=a(N-1)=\sqrt{2/N}$.


\item  定义（矩阵形式）：记 $C$ 是 一维 DCT 的变换矩阵，则从 $f$ 到 $F$ 的二维 DCT 变换可以写成
{\color{red}
\begin{eqnarray*}
F=C(Cf\, ^t)^t,
\end{eqnarray*}
}
其中 $f\, ^t$ 是矩阵 $f$ 的转置。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{3.6. 二维的离散余弦变换}
\begin{frame}{4.2. 变换矩阵 C }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  长度为 $N$ 的一维 DCT 的变换矩阵 $C$ 是一个正交矩阵，
\begin{eqnarray*}
C=\sqrt{\frac{2}{N}}\cdot \begin{bmatrix}
\frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} & \cdots & \frac{1}{\sqrt{2}} \\
\cos\frac{\pi}{2N} & \cos\frac{3\pi}{2N} & \cdots & \cos\frac{(2N-1)\pi}{2N}  \\
\cos\frac{2\pi}{2N} & \cos\frac{6\pi}{2N} & \cdots & \cos\frac{2(2N-1)\pi}{2N}  \\
\vdots & \vdots & \cdots & \vdots \\
\cos\frac{(N-1)\pi}{2N} & \cos\frac{(N-1)3\pi}{2N} & \cdots & \cos\frac{(N-1)(2N-1)\pi}{2N}  \\
\end{bmatrix}
\end{eqnarray*}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.7. }
\begin{frame}{4.3. 8x8情形的离散余弦变换DCT和逆变换IDCT}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
%\item 定义：

\item {\color{red}问题8：设 $N=8$, 写出二维情形的离散余弦变换及其逆变换。}

\item 解答：设 $C(0)=1/\sqrt{2}$, $C(1)=\cdots=C(7)=1$, 则有 
\begin{eqnarray*}
F(u,v) &=& \frac{1}{4}C(u)C(v) \sum\limits_{x=0}^{7} \sum\limits_{y=0}^{7} f(x,y)
\cos \left[ \frac{\pi(2x+1)u}{16} \right] 
\cos \left[ \frac{\pi(2y+1)v}{16} \right]. \\
f(x,y) &=&  \frac{1}{4} \sum\limits_{u=0}^{7} \sum\limits_{v=0}^{7} C(u)C(v) F(u,v)
\cos \left[ \frac{\pi(2x+1)u}{16} \right]  
\cos \left[ \frac{\pi(2y+1)v}{16} \right].
\end{eqnarray*}

\item  注：\\ %这两个公式将在编程中用到。\\
从 $f(x,y)$ 到 $F(u,v)$ 是离散余弦变换 DCT. \\
从 $F(u,v)$ 到 $f(x,y)$ 是逆变换 IDCT. 

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.7. }
\begin{frame}{4.4. 量子化 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  定义：对每个频域数据 $F(u,v)$, 除以相应的量子化尺度 $Q(u,v)$, 再取最接近的整数，这个过程称为量子化。

%Quantization is defined as division of each DCT coefficient by its corresponding quantizer step size, followed by rounding to the nearest integer. 
{\color{red}
\begin{eqnarray*}
F^Q(u,v) = \left[ \frac{F(u,v)}{Q(u,v)} \right]. 
\end{eqnarray*}
}

\item  注：这一步保留了主要信息，反映了这是有损失的压缩。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.7. }
\begin{frame}{4.5. 量子化的例子 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问题：下述数据 $F$ 经过量子化尺度 $Q$ 之后的结果是多少？
\begin{eqnarray*}
&& F=\begin{bmatrix} 236&-1&-12&-5 \\ -23&-17&-6&-3 \\ -11&-9&-2&2 \\ -7&-2&0&2 \end{bmatrix}, %\hspace{0.3cm}
Q=\begin{bmatrix} 16&11&10&16 \\ 12&12&14&19 \\ 14&13&16&24 \\ 14&17&22&29 \end{bmatrix}.  %\hspace{0.3cm}
\end{eqnarray*}

\item  解答：将 $F$ 的每个元素除以 $Q$ 的相应元素，然后取最近的整数，可得
\begin{eqnarray*}
F^{Q	}=\begin{bmatrix} 15&0&-1&0 \\ -2&-1&0&0 \\ -1&-1&0&0 \\ 0&0&0&0 \end{bmatrix}. 
\end{eqnarray*}

\item  注：经过这一步，从保存16个整数，简化为保存大约8个整数。

%import numpy as np
%F=np.array([
%       [236,-1,-12,-5], 
%       [-23,-17,-6,-3],
%       [-11,-9,-2,2],
%       [-7,-2,0,2] 
%       ])
%Q=np.array([
%        [16,11,10,16],
%        [12,12,14,19],
%        [14,13,16,24],
%        [14,17,22,29]
%        ])
%F_Q=np.round(F/Q,0)
%F_Q[F_Q==-0.]=0.
%print(F_Q)


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}{2.1. Processing Steps for DCT-Based Coding}
\begin{frame}{4.6. 根据二维离散余弦变换的图像存储过程}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}
\item  This figure shows the key processing steps which are the heart of the DCT-based modes of operation. 

%\item  These figures illustrate the special case of single-component (grayscale) image compression. 

\item  The reader can grasp the essentials of DCT-based compression by thinking of it as essentially compression of a stream of 8x8 blocks of grayscale image samples. 

%\item  Color image compression can then be approximately regarded as compression of multiple grayscale images, which are either compressed entirely one at a time, or are compressed by alternately interleaving 8x8 sample blocks from each in turn.

\end{itemize}

\vspace{0.3cm}

\begin{center}
\usetikzlibrary {graphs,shapes.geometric} 
\tikz {
%\node [rectangle, draw] (sid) at (0,0) {Source 8x8 Image}; 
%\node [rectangle, draw] (fdct) at (3,0) {FDCT}; 
%\node [rectangle, draw] (q) at (5.3,0) {Quantizer}; 
%\node [rectangle, draw] (ee) at (8.7,0) {Entropy Encoder}; 
%\node [rectangle, draw] (cid) at (11,-1) {Compressed Data}; 
%\node [rectangle, draw] (ed) at (8.7,-2) {Entropy Decoder}; 
%\node [rectangle, draw] (dq) at (5.3,-2) {Dequantizer}; 
%\node [rectangle, draw] (idct) at (3,-2) {IDCT}; 
%\node [rectangle, draw] (rid) at (0,-2) {Reconstructed Image}; 
\node [rectangle, draw] (sid) at (0.5,0) { 8x8源图像}; 
\node [rectangle, draw] (fdct) at (3.5,0) {余弦变换}; 
\node [rectangle, draw] (q) at (6,0) {量子化}; 
\node [rectangle, draw] (ee) at (8.5,0) {熵编码}; 
\node [rectangle, draw] (cid) at (11,-1) {压缩数据}; 
\node [rectangle, draw] (ed) at (8.5,-2) {熵解码}; 
\node [rectangle, draw] (dq) at (6,-2) {逆量子化}; 
\node [rectangle, draw] (idct) at (3.5,-2) {逆变换}; 
\node [rectangle, draw] (rid) at (0.5,-2) {重建图像}; 
\graph { (sid) -> (fdct) -> (q) -> (ee) -> (cid) };
\graph { (cid) -> (ed) -> (dq) -> (idct) -> (rid) };
}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.20. }
\begin{frame}{4.7. 一个二维DCT的例子 (by Majid Rabbani)}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

 \begin{figure}
 \centering
 \includegraphics[height=0.75\textheight, width=0.96\textwidth]{dct-quantization-example.png}
% \caption{ }
 \end{figure}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.20. }
\begin{frame}{4.8. 一个二维DCT的例子 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item {\color{red}问题9：编程实现Rabbani的这个例子。}

\begin{enumerate}
\item  (a) -> (b) 
\item  (b) -> (d) 
\item  (d) -> (e) 
\item  (e) -> (f)
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.9. 问题9的解答（R语言，DCT第一部分）}
%\begin{frame}{3.6. 问题9的解答（R语言） }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{lstlisting}[language={R}]
b1<-c(139,144,149,153,155,155,155,155)
b2<-c(144,151,153,156,159,156,156,156)
b3<-c(150,155,160,163,158,156,156,156)
b4<-c(159,161,162,160,160,159,159,159)
b5<-c(159,160,161,162,162,155,155,155)
b6<-c(161,161,161,161,160,157,157,157)
b7<-c(162,162,161,163,162,157,157,157)
b8<-c(162,162,161,161,163,158,158,158)
B1<-matrix(rbind(b1,b2,b3,b4,b5,b6,b7,b8),nrow=8)
B<-B1-128
C<-c(1/sqrt(2),rep(1,7))
\end{lstlisting}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.10. 问题9的解答（R语言，DCT第二部分）}
%\begin{frame}{3.6. 问题9的解答（R语言） }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{lstlisting}[language={R}]
Bdct<-matrix(data=0,nrow=8,ncol=8)
for (u in 1:8)
for (v in 1:8)
for (x in 1:8)
for (y in 1:8)
Bdct[u,v] <- Bdct[u,v]+B[x,y]*cos(pi*(2*x-1)*(u-1)/16)
             *cos(pi*(2*y-1)*(v-1)/16)

for (u in 1:8)
for (v in 1:8)
Bdct[u,v]<-Bdct[u,v]*C[u]*C[v]/4
\end{lstlisting}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.11. 问题9的解答（R语言，Quantization部分）}
%\begin{frame}{3.6. 问题9的解答（R语言） }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{lstlisting}[language={R}]
q1<-c(16,11,10,16,24,40,51,61)
q2<-c(12,12,14,19,26,58,60,55)
q3<-c(14,13,16,24,40,57,69,56)
q4<-c(14,17,22,29,51,87,80,62)
q5<-c(18,22,37,56,68,109,103,77)
q6<-c(24,35,55,64,81,104,113,92)
q7<-c(49,64,78,87,103,121,120,101)
q8<-c(72,92,95,98,112,100,103,99)
Q<-matrix(rbind(q1,q2,q3,q4,q5,q6,q7,q8),nrow=8)

BdctQ<-matrix(data=0,nrow=8,ncol=8)
for (u in 1:8)
for (v in 1:8)
BdctQ[u,v] <- round(Bdct[u,v]/Q[u,v])
\end{lstlisting}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.12. 问题9的解答（Python - 1） }
%\begin{frame}{ }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
import numpy as np

#输入数据矩阵
b1=[139,144,149,153,155,155,155,155]
b2=[144,151,153,156,159,156,156,156]
b3=[150,155,160,163,158,156,156,156]
b4=[159,161,162,160,160,159,159,159]
b5=[159,160,161,162,162,155,155,155]
b6=[161,161,161,161,160,157,157,157]
b7=[162,162,161,163,162,157,157,157]
b8=[162,162,161,161,163,158,158,158]
B=np.array([b1,b2,b3,b4,b5,b6,b7,b8])
print(B)

\end{python}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.13. 问题9的解答（Python - 2） }
%\begin{frame}{ }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
B=B-128
#DCT变换矩阵
C=np.zeros([8,8],dtype=float)
for t in range(8):
    for x in range(8):
        C[t,x]=np.cos(np.pi*t*(2*x+1)/2/8)
C[0,:]=1/np.sqrt(2)
C=C*np.sqrt(2/8)

#画出DCT变换矩阵
import matplotlib.pyplot as plt 
plt.imshow(C)

#对输入数据进行二维DCT变换
Bdct=np.dot(C,np.dot(C,B.T).T)
\end{python}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.14. 问题9的解答（Python - 3） }
%\begin{frame}{ }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
#量子化尺度矩阵
q1=[16,11,10,16,24,40,51,61]
q2=[12,12,14,19,26,58,60,55]
q3=[14,13,16,24,40,57,69,56]
q4=[14,17,22,29,51,87,80,62]
q5=[18,22,37,56,68,109,103,77]
q6=[24,35,55,64,81,104,113,92]
q7=[49,64,78,87,103,121,120,101]
q8=[72,92,95,98,112,100,103,99]
Q=np.array([q1,q2,q3,q4,q5,q6,q7,q8])

#量子化结果，由此开始霍夫曼编码保存
BdctQ=np.round(Bdct/Q,0)
BdctQ[BdctQ==-0.]=0.
print(BdctQ)
\end{python}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.15. 问题9的解答（Python - 4） }
%\begin{frame}{ }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
#开始解码，逆量子化
Edct=BdctQ*Q
#逆二维DCT变换
E=np.dot(C.T,np.dot(Edct,C))
E=E+128

#显示原始数据和压缩后还原的数据
fig=plt.figure()
ax=fig.add_subplot(121)
ax.imshow(B)
bx=fig.add_subplot(122)
bx.imshow(E)
\end{python}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.2. }
\begin{frame}{4.16. 原始数据 $B$ 和还原后的数据 $E$ }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

 \begin{figure}
 \centering
 \includegraphics[height=0.6\textheight, width=0.8\textwidth]{rabbani-example-4-16.png}
% \caption{ }
 \end{figure}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.20. }
\begin{frame}{5.1. 两张 JPG 图片 (pic by wikipedia)}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

 \begin{figure}
 \centering
 \includegraphics[height=0.5\textheight, width=0.45\textwidth]{jpeg-average-quality.jpg}
 \hspace{0.2cm}
 \includegraphics[height=0.5\textheight, width=0.45\textwidth]{jpeg-lowest-quality.jpg}
 \caption{Left: average quality, 15K; Right: lowest quality, 2K. }
 \end{figure}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{5.2. 问题10 }
%\begin{frame}{3.11. 问题10 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item {\color{red}问题10：将上述JPG图像用离散余弦变换处理，舍弃高频部分，然后再用逆变换恢复图像。图像文件参见课程资料。}

\item 参考：

\begin{python}
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
x=mpimg.imread('jpeg-average-quality.jpg')
type(x)
x.shape
y=x[:,:,1]
plt.imshow(y)
z=y[32:128,96:256]
plt.imshow(z/2)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{5.3. 问题10a }
%\begin{frame}{3.11. 问题10 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
######################################################
####载入图像，截取32x32x1的一块
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

data=mpimg.imread('jpeg-average-quality.jpg')
A=data[96:128,96:128,1]
plt.imshow(A)

######################################################
####不使用科学计数法显示计算结果
np.set_printoptions(suppress=True)

\end{python}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{5.4. 问题10b }
%\begin{frame}{3.11. 问题10 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
######################################################
####截取8x8的一块，数据为矩阵B,其离散余弦变换后的矩阵为Bdct
B=A[0:8,0:8]; Bdct=np.zeros((8,8)) 
C=np.ones(8); C[0]=np.sqrt(1/2)

for u in range(8):
    for v in range(8):
        for x in range(8):
            for y in range(8):
                Bdct[u,v]=Bdct[u,v]+B[x,y]*\
                np.cos(np.pi*(2*x+1)*u/16)*\
                np.cos(np.pi*(2*y+1)*v/16)        
for u in range(8):
    for v in range(8):
        Bdct[u,v]=Bdct[u,v]*C[u]*C[v]/4
\end{python}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{5.5. 问题10c }
%\begin{frame}{3.11. 问题10 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
######################################################
####保留DCT变换后的矩阵的左上角的10个数字，其余设置为零，
####得到的矩阵记为Cdct
Cdct=np.zeros((8,8))
for u in range(8):
    for v in range(8):
        if u+v<=3:
            Cdct[u,v]=Bdct[u,v]
        else:
            Cdct[u,v]=0

np.round(Cdct,0)
\end{python}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{5.6. 问题10d }
%\begin{frame}{3.11. 问题10 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
######################################################
####从CDct数据出发，进行逆变换，得到的矩阵记为Edct
Edct=np.zeros((8,8))
for x in range(8):
    for y in range(8):
        for u in range(8):
            for v in range(8):
                Edct[x,y]=Edct[x,y]+C[u]*C[v]*Cdct[u,v]*\
                np.cos(np.pi*(2*x+1)*u/16)*\
                np.cos(np.pi*(2*y+1)*v/16)/4

\end{python}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{5.7. 问题10e }
%\begin{frame}{3.11. 问题10 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
######################################################
####画图比较原始图像，与经过DCT变换与置零与逆变换后的图像
plt.subplot(121); plt.imshow(B)
plt.subplot(122); plt.imshow(np.uint8(np.round(Edct,0)))
plt.savefig('compare-two-8x8-matrices.png')
\end{python}

 %\begin{figure}
 %\centering
 \includegraphics[height=0.5\textheight, width=0.5\textwidth]{compare-two-8x8-matrices.png}
% \caption{ }
 %\end{figure}
 
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{5.8. 问题10f }
%\begin{frame}{3.11. 问题10 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容


\begin{itemize}
\item {\color{red}问题10f.1: 在问题10c中改变保留数字的多少，比较复原效果。}
\item {\color{red}问题10f.2: 将矩阵A分成16个8x8的小块，分别用DCT变换、置零、逆变换，再合并成一个32x32的图片。比较两个图片的差别。}
\item {\color{red}问题10f.3: 对原始图片data进行测试。}

\end{itemize}


 
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.20. }
\begin{frame}{参考文献}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{thebibliography}{99}

\bibitem{gkw}  Gregory K. Wallace.  
\emph{The JPEG Still Picture Compression Standard}. IEEE Transactions on Consumer Electronics, 1991. 

\bibitem{abw} Andrew B. Watson. 
\emph{Image Compression Using the Discrete Cosine Transform}. Mathematica Journal, 4(1), 1994, 81-88.

%\bibitem{sak} Syed Ali Khayam. 
%\emph{The Discrete Cosine Transform: Theory and Application}. ECE Notes, Michigan State University, March 2003. 

%\bibitem{ijg} \url{http://ijg.org}.

\bibitem{jpeg} \url{https://jpeg.org}. 

\bibitem{sauer} Timothy Sauer 著, 裴玉茹等译. \emph{数值分析}. 机械工业出版社. 2018年8月第1版.

\bibitem{taubman} David S. Taubman, Michael W. Marcellin
\emph{JPEG 2000: Image Compression Fundamentals, Standards and Practice.} Kluwer Academic Publishers, November 2001. 

%\bibitem{fec-andes} Frederic Edwin Church. \emph{Heart of the Andes}. 1859. 


\end{thebibliography}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\end{document}










